package com.rongj.generation.beetl;

import org.beetl.core.Configuration;
import org.beetl.core.GroupTemplate;
import org.beetl.core.Template;
import org.beetl.core.resource.ClasspathResourceLoader;

import java.io.*;
import java.util.Map;

public class BeetlUtil {

    private static GroupTemplate groupTemplate;

    public static GroupTemplate getGroupTemplate()  {
        if(groupTemplate == null){
            ClasspathResourceLoader resourceLoader = new ClasspathResourceLoader("template/");
            Configuration cfg = null;
            try {
                cfg = Configuration.defaultConfiguration();
                groupTemplate = new GroupTemplate(resourceLoader, cfg);
                return groupTemplate;
            } catch (IOException e) {
                e.printStackTrace();

            }
        }
        return groupTemplate;
    }

    public static void outputTemplate(Map<String,Object> filterMap, String templateName, File file){
        Template t = getGroupTemplate().getTemplate(templateName);
        t.binding(filterMap);
        try {
            OutputStream  outputStream = new FileOutputStream(file);
            t.renderTo(outputStream);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

    }
}
